<template>
  <!-- 充值套餐 -->
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <!-- <template slot="menuLeft">
        <div class="curd-foter">
          <div class="redFontBox">缴费笔数：<span class="redFont">{{ exhibition(countData.totalTradeCount) || 0}}</span> 笔</div>
          <div class="redFontBox">缴费金额：<span class="redFont">{{ exhibition(countData.totalRealAmount ) || 0 }}</span> 元</div>
        </div>
      </template> -->
      <template slot="menuRight">
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.trade_export"
          @click="handleExport"
          >导出
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="permissionList.exportBtn"
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleDetail(scope.row)"
          >详 情</el-button
        >
      </template>
      
      <template slot-scope="{disabled,size}" slot="beginTimeSearch">
        <el-date-picker v-model="search.beginTime" type="datetime" value-format='yyyy-MM-dd HH:mm:ss' @change="beginTimeChange" placeholder="请输入 交易开始日期"></el-date-picker>
      </template>
      <template slot-scope="{disabled,size}" slot="endTimeSearch">
        <el-date-picker v-model="search.endTime" type="datetime" default-time="23:59:59" value-format='yyyy-MM-dd HH:mm:ss' @change="endTimeChange" placeholder="请输入 交易结束日期"></el-date-picker>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { flowpackage } from "@/api/rb-oem/iot/iot_group";
import { mapGetters } from "vuex";
// import { getToken } from "@/util/auth";
// import { dateFormat } from "@/util/date";

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120, //弹出表单的标题的统一宽度
        searchLabelWidth: 120, //搜索框标题宽度
        menuWidth: 100, //操作栏宽度
        menu:false,
        emptyBtnText:'重置',
        // searchIcon:true,
        // searchIndex:3,
        column: [
           {
            label: "运营商",
            prop: "telecomOperatorName",
            type: "input",
            overHidden:true
          },
          
          {
            label: "产品 ID",
            prop: "productId",
            type: 'input',
            // search: true,
            overHidden:true
          },
          {
            label: "流量包价格(元)",
            prop: "price",
            type: "input",
            // search: true
          },
          {
            label: "产品名称",
            prop: "productName",
            type: "input",
            overHidden:true
          },
          {
            label: "流量包大小(MB)",
            prop: "flow",
            type: "input"
          },
          {
            label: "流量包类型",
            prop: "flowType",
            type: 'select',
            // search: true,
            dicData: [{
              label: '公网',
              value: '0'
            }, {
              label: '定向',
              value: '1'
            }]
          }
        ]
      },
      data: [],
      countData: {},
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.iot_rechargePackage_view, false),
        // exportBtn: this.vaildData(this.permission.iot_recharge_export, false),
      };
    },
    exhibition() {
      return (val) => {
        if (val == "" || val == "-1") {
          return (val = 0);
        } else {
          return val;
        }
      };
    },
  },
  watch: {},
  created(){},
  methods: {
    beginTimeChange() {
      if(this.search.endTime){
        if(Date.parse(this.search.beginTime) > Date.parse(this.search.endTime)){
          this.$alert('统计开始日期不能大于结束日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.beginTime = ""))
          .catch(() => (this.search.beginTime = ""))
        }
      }
    },
    endTimeChange() {
      if(this.search.beginTime){
        if(Date.parse(this.search.endTime) < Date.parse(this.search.beginTime)){
          this.$alert('统计结束日期不能小于开始日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.endTime = ""))
          .catch(() => (this.search.endTime = ""))
        }
      }
    },
    forInSearch(){
      for(let key in this.search){
        if(this.search[key]){
          if(key.indexOf("$") == -1 ){
            this.query[key] = this.search[key]
          }
        }
      }
    },
    /**
     * @func 初始化
     * @desc 表格初始化查询数据
     * */
    onLoad(page, params = {}) {
      this.loading = true;
      flowpackage(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.products.length;
        this.data = data.products;
        this.loading = false;
        this.selectionClear();
      });

      /**
       * @func 统计
       * @desc 根据条件获取统计
       * */
      // getListCount(Object.assign(params, this.query)).then((res) => {
      //   this.countData = res.data.data;
      // });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    }
  }
};
</script>

<style lang="scss" scoped>
.curd-foter {
  padding-top: 10px;
  display: flex;
  flex-wrap: wrap;
  .redFontBox {
    margin-right: 40px;
    .redFont {
      color: red;
    }
  }
}

/deep/ .el-tag--dark.el-tag--success {
  border: none !important;
}
</style>
